package com.hbusa.cotroller.chapter;


import com.hbusa.dto.TbChapter;
import com.hbusa.dto.TbNovels;
import com.hbusa.service.impl.*;
import com.hbusa.service.service.impl.NovelsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/doNovelChapter")
public class ChapterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        /**
         * input 统称为表单空间
         * type="text" 表示这个控件是输入框
         * <input type="text" name="novel_id">
         * req.getParameter("novel_id") 可以获取名称为novel_id的这个表单控件的值
         * doNovelChapter?novel_id=xiaoxin req.getParameter("novel_id")可以获取到 xiaoxin 这个字符串
         */
        String novel_id = req.getParameter("novel_id");
        System.out.println("novel_id:" + novel_id);
        HttpSession session= req.getSession();
        NovelsServiceImpl novelsService = new NovelsServiceImpl();
        ArrayList<TbChapter> tbChapters = novelsService.queryChapterByNovelId(Long.parseLong(novel_id));
//        for (TbChapter tbChapter : tbChapters) {
//            System.out.println(tbChapter);
//        }

        try {
            TbNovels novel = novelsService.findNovelById(Integer.parseInt(novel_id));
            System.out.println(novel);
            session.setAttribute("novel", novel);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        session.setAttribute("tbChapters", tbChapters);
        resp.sendRedirect("/chapter.jsp");
//        session.setAttribute("novelName",novelName);
//        session.setAttribute("author",author);
//        session.setAttribute("describes",describes);
//
//        resp.sendRedirect("chapter.jsp");
    }
}
